<?php


namespace apps\marriage\app\mp\controller;


use apps\marriage\core\logic\AuthLogic;
use apps\marriage\core\model\MemberAuth;
use apps\marriage\core\service\MemberAuthService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\SiteConfigService;
use think\Exception;
use think\Request;

class MemberAuthController extends BaseMarriageMpController
{
    /**
     * @var AuthLogic
     */
    private $authLogic;

    /**
     * @var MemberAuthService
     */
    private $memberAuthService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    /**
     * @var SiteConfigService
     */
    private $siteConfigService;

    protected function init()
    {
        parent::init();
        $this->authLogic = logic('Auth', LOGIC_NAMESPACE);
        $this->memberAuthService = service('MemberAuth', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
        $this->siteConfigService = service('SiteConfig', SERVICE_NAMESPACE);
    }

    public function my()
    {
        $auth = $this->memberAuthService->info(['user_id' => $this->user['id']]);
        if($auth) {
            $auth = arrayExcept($auth, ['name', 'idcard', 'idcard_ocr_front', 'idcard_ocr_back']);
            $auth = $this->memberAuthService->format($auth);
        }
        return $this->success(['auth' => $auth]);
    }

    public function face_detect_url(Request $request)
    {
        $name = $request->param('name');
        $idcard = $request->param('idcard');
        $url = $request->param('url');

        $member = $this->memberProfileService->getByUserId($this->user['id']);
        if($member['realname_auth_status'] == 1) {
            throw new Exception('已完成实名认证');
        }

        $url = request()->scheme() . '://' . request()->host() . '/' . getContextPath() . 'mp.php/member_auth/face_detect_result?user_id=' . $this->user['id'];

        $res = $this->authLogic->getFaceDetectUrl($this->user['id'] ,$name, $idcard, $url);
        return $this->success([
            'url' => $res
        ]);
    }

    public function face_detect_result(Request $request)
    {
        $userId = $request->param('user_id', 0, 'intval');

        $mpSite = $this->siteConfigService->getValueByCode('mp_site');
        $href = $mpSite . '/my_index';

        $member = $this->memberProfileService->getByUserId($userId);
        if($member['realname_auth_status'] == 1) {
            $str = '<script>alert("已完成了认证");window.location.href="'.$href.'";</script>';
            die($str);
        }

        $result = $this->memberAuthService->onFaceIdDetectCompleted($userId);

        ob_end_clean();
        if($result) {
            $str = '<script>alert("认证成功");window.location.href="'.$href.'";</script>';
            die($str);
        } else {
            $str = '<script>alert("认证失败");window.location.href="'.$href.'";</script>';
            die($str);
        }
    }

//    public function face_detect_result()
//    {
//        $res = $this->memberAuthService->onFaceIdDetectCompleted($this->user['id']);
//        return $this->success([
//            'result' => $res
//        ]);
//    }

    public function save(Request $request)
    {
        $type = $request->param('type');
        $fileId = $request->param('file_id');

        $member = $this->memberProfileService->getByUserId($this->user['id']);
        if($member['realname_auth_status'] == 0) {
            throw new Exception('请先完成实名认证');
        }
        $data = [];
        if($type == 'car') {
            $data['car_auth_file_id'] = $fileId;
            $data['car_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
            $data['car_auth_upload_time'] = time();
        } else if($type == 'house') {
            $data['house_auth_file_id'] = $fileId;
            $data['house_auth_location'] = $request->param('house_location', 0);
            $data['house_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
            $data['house_auth_upload_time'] = time();
        } else if($type == 'education') {
            $data['education_auth_file_id'] = $fileId;
            $data['education_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
            $data['education_auth_upload_time'] = time();
        } else if($type == 'marriage') {
            $data['marriage_auth_file_id'] = $fileId;
            $data['marriage_auth_status'] = MemberAuth::AUTH_STATUS_WAIT;
            $data['marriage_auth_upload_time'] = time();
        }

        $this->memberAuthService->update($data, ['user_id' => $this->user['id']]);
        return $this->success();
    }
}